package day_2022_9_to_12.Day;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author haomin
 * @date 2022/09/15 23:12
 **/
public class Day13_4_Num438_FindAnagrams {
    class Solution {
        public List<Integer> findAnagrams(String s, String p) {
            List<Integer> list = new ArrayList<>();
            if(s.length() < p.length()) return list;
            int[] pArr = new int[26];
            int[] sArr = new int[26];
            int l = 0, r = -1;
            for(int i = 0; i < p.length(); ++i){
                pArr[p.charAt(i)-'a']++;
                sArr[s.charAt(i)-'a']++;
                r++;
            }
            if(Arrays.equals(pArr, sArr)) list.add(l);
            while(r < s.length() - 1){
                sArr[s.charAt(l)-'a']--;
                l++;
                r++;
                sArr[s.charAt(r)-'a']++;
                if(Arrays.equals(pArr, sArr)) list.add(l);
            }
            return list;
        }
    }
}